package com.b2c.product.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.b2c.product.mapper.BranchMapper;
import com.b2c.product.pojo.dto.BranchDto;
import com.b2c.product.pojo.entity.Branch;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.b2c.product.service.BranchService;
import content.enumeration.BranchStatus;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import result.Result;
import util.AdminUserToken;
import util.ThreadLocalUtil;

import static content.common.ResultMessage.ONLY_BRANCH;
import static content.common.ResultMessage.SERVER_ERROR;

/**
 * @author Administrator
 * @description 针对表【mall_branch(品牌表)】的数据库操作Service实现
 * @createDate 2024-03-20 17:28:30
 */
@Service
@Slf4j
public class BranchServiceImpl extends ServiceImpl<BranchMapper, Branch>
        implements BranchService {
    @Autowired
    BranchMapper branchMapper;

    /**
     * 商家查看品牌
     *
     * @return
     */
    @Override
    public Result getBranchByAdminId() {
        //获取商家id
        Integer adminId = new ThreadLocalUtil<AdminUserToken>().get().getId();
        log.info("商家查看品牌{}",adminId);
        //查询数据库
        QueryWrapper<Branch> branchUpdateWrapper = new QueryWrapper<Branch>().eq("admin_id", adminId);
        Branch branch;
        try {
            branch = getOne(branchUpdateWrapper);
        } catch (Exception e) {
            log.error("商家查看商品失败:{}", e);
            return Result.fail(SERVER_ERROR);
        }
        return Result.success(branch);
    }

    /**
     * 添加品牌
     *
     * @param branchDto
     * @return
     */
    @Override
    public Result insert(BranchDto branchDto) {
        log.info("添加品牌,{}",branchDto);
        //获取商家id
        Integer adminId = new ThreadLocalUtil<AdminUserToken>().get().getId();
        Branch branch = BeanUtil.copyProperties(branchDto, Branch.class);
        branch.setId(adminId);
        //查看数据库中的商家品牌数
        QueryWrapper<Branch> queryWrapper = new QueryWrapper<Branch>().eq("admin_id", adminId);
        int count = count(queryWrapper);
        if (count > 0) {
            return Result.fail(ONLY_BRANCH);
        }
        //插入到数据库
        save(branch);
        return Result.success();
    }

    /**
     * 删除品牌
     *
     * @return
     */
    @Override
    public Result delete() {
        Integer adminId = new ThreadLocalUtil<AdminUserToken>().get().getId();
        log.info("删除品牌,adminId:{}",adminId);
        //通过商家id删除品牌
        QueryWrapper<Branch> queryWrapper = new QueryWrapper<Branch>().eq("admin_id",adminId);
        remove(queryWrapper);
        return Result.success();
    }

    /**
     * 通过商家id查询品牌
     *
     * @param adminId
     * @return
     */
    @Override
    public Result getBranchByAdminId(Integer adminId) {
        log.info("查询品牌{}",adminId);
        QueryWrapper<Branch> queryWrapper = new QueryWrapper<Branch>().eq("admin_id", adminId);
        Branch branch = getOne(queryWrapper);
        return Result.success(branch);
    }

    /***
     * 分页查询所有的品牌
     * @param page
     * @return
     */
    @Override
    public Result queryAllBranch(IPage<Branch> page) {
        log.info("分页查询所有品牌:{}",page);
        IPage<Branch> branchIPage = branchMapper.selectPage(page, Wrappers.emptyWrapper());
        return Result.success(branchIPage);
    }

    /**
     * 更改品牌状态
     *
     * @param status
     * @param adminId
     * @return
     */
    @Override
    public Result updateStatus( BranchStatus status, Integer adminId) {
        log.info("更改品牌状态,adminId:{},status:{}",adminId,status);
        UpdateWrapper<Branch> updateWrapper = new UpdateWrapper<Branch>().set("status", status.getCode()).eq("admin_id", adminId);
        update(updateWrapper);
        return Result.success();
    }


}




